<?php

/** 
 * @author shf
 * 
 */
class MySqlHelper {
	/**
	 * 连接对象
	 * @var mysqliconnection
	 */
	private $objConn = null;
	private $resultsList=NULL;
	function __construct() {
		$this->Connect();
	}
	private function Connect()
	{
		$this->objConn = new mysqli('bdm-010.hichina.com','bdm0100060','jl198704','bdm0100060_db');
		//$this->objConn = new mysqli('127.0.0.1','root','root','rbsd');
		$this->objConn->set_charset('utf8');
		if(!$this->objConn)
		{
			throw new Exception('数据库连接失败!',0);
		}
	}
	/**
	 * 获取数据集
	 * @param Sql $query
	 * @throws Exception
	 */
	function GetRows($query)
	{
		$this->resultsList = $this->objConn->query($query);
		if(!$this->resultsList)
		{
			throw new Exception('执行Sql失败！');
		}
		return $this->resultsList;
	}
	/**
	 * 获取数据集的关联数组
	 * @param Sql $query
	 * @return 关联数组:
	 */
	function GetJsonData($query){
		$this->GetRows($query);
		$arr = array();
	    while ($row=$this->resultsList->fetch_assoc())
	    {
		    array_push($arr, $row); 
	    }
	    return $arr;
	}
	/**
	 * 释放结果集
	 */
	function FreeResult()
	{
		if(!empty($this->resultsList)){
		    $this->resultsList->free();
		}
	}
	/**
	 * 执行查询
	 * @param 查询语句 $query
	 */
	function ExecSql($query)
	{
		return $this->objConn->query($query);
	}
	/**
	 * 判断数据是否存在
	 * @param 查询语句 $query
	 * @return boolean
	 */
	function Exists($query)
	{
		return $this->objConn->query($query)->num_rows>0;
	}
	function GetRow($query)
	{
		return $this->objConn->query($query)->fetch_array(MYSQLI_ASSOC);
	}
	function __destruct() {
		$this->FreeResult();
		$this->objConn->close();
	}
}
?>